package com.example.crm.service;

import com.example.crm.entity.Customer; // <-- 应该是 Customer
import com.example.crm.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CustomerService {
    @Autowired
    private CustomerRepository customerRepository;

    public List<Customer> findAll() {

        return customerRepository.findAll();
    }

    public Optional<Customer> findById(Integer id) {

        return customerRepository.findById(id);
    }

    public Customer save(Customer customer) {
        validatePhone(customer.getCustomerphone());
        return customerRepository.save(customer);
    }

    public void deleteById(Integer id) {

        customerRepository.deleteById(id);
    }
    private void validatePhone(String phone) {
        if (phone != null && !phone.isEmpty() && phone.length() != 11) {
            throw new IllegalArgumentException("电话号码位数错误，必须是11位！");
        }
    }
}